#include "stm32f10x.h"                  // Device header
#include "PWM.h"

// 使用全局配置
extern PWM_Channel_t PWM_Channels[];
extern const uint8_t PWM_CHANNEL_COUNT;

//// PWM配置数组 (支持最多4个电机)
//PWM_Channel_t PWM_Channels[] = {		
//    // 电机2: TIM1 CH1 (PA8)										// 1高级定时器,0通用定时器
//    {TIM1, TIM_Channel_2, TIM_SetCompare4, GPIOA, GPIO_Pin_11, 1}// 左电机
//                                                                 
//    // 电机1: TIM1 CH2 (PA9)                                       
//    {TIM1, TIM_Channel_1, TIM_SetCompare1, GPIOA, GPIO_Pin_8, 1},// 右电机
//};
//// 电机数量
//#define PWM_CHANNEL_COUNT (sizeof(PWM_Channels)/sizeof(PWM_Channels[0]))

void PWM_Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    
    // 1. 时钟使能
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE);
    
    // 2. GPIO配置 (复用推挽输出)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    
    for(uint8_t i = 0; i < PWM_CHANNEL_COUNT; i++) {
        GPIO_InitStructure.GPIO_Pin = PWM_Channels[i].GPIO_Pin;
        GPIO_Init(PWM_Channels[i].GPIOx, &GPIO_InitStructure);
    }
    
    // 3. 定时器基础配置
    TIM_TimeBaseStructure.TIM_Period = 100 - 1;           // PWM频率 = 72MHz / (99+1) = 72kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    
    // 4. PWM输出配置
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
    
    // 为每个通道单独配置
    for(uint8_t i = 0; i < PWM_CHANNEL_COUNT; i++) {
        switch(PWM_Channels[i].TIM_Channel) {
            case TIM_Channel_1:
                TIM_OC1Init(TIM1, &TIM_OCInitStructure);
                TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
                break;
            case TIM_Channel_2:
                TIM_OC2Init(TIM1, &TIM_OCInitStructure);
                TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
                break;
            case TIM_Channel_3:
                TIM_OC3Init(TIM1, &TIM_OCInitStructure);
                TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
                break;
            case TIM_Channel_4:
                TIM_OC4Init(TIM1, &TIM_OCInitStructure);
                TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
                break;
        }
    }
	
	// 使能预装载寄存器
    TIM_ARRPreloadConfig(TIM1, ENABLE);
	
	// 高级定时器必须开启主输出
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
	
	/*TIM使能*/
	TIM_Cmd(TIM1, ENABLE);			//使能TIM1，定时器开始运行
}

/**
  * 函    数：PWM设置CCR
  * 参    数：Compare 要写入的CCR的值，范围：0~100
  * 返 回 值：无
  * 注意事项：CCR和ARR共同决定占空比，此函数仅设置CCR的值，并不直接是占空比
  *           占空比Duty = CCR / (ARR + 1)
  */

void PWM_SetDuty(int8_t motor, float PWM) {
    if(motor >= PWM_CHANNEL_COUNT) return;
    
    // 限制占空比范围
//    if(duty > 100.0f) duty = 100.0f;
//    if(duty < -100.0f) duty = -100.0f;
    
    // 计算PWM值 (0-999)
//    uint16_t pulse = (uint16_t)(fabs(duty) * 9.99f);
    
    // 使用函数指针设置比较值
    PWM_Channels[motor].TIM_SetCompare(PWM_Channels[motor].TIMx, PWM);
}

